﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Web.Configuration
Imports System.Text.RegularExpressions
Imports System.Security.Cryptography



''' <summary>
''' Class that exposes various shared utility methods
''' </summary>
''' <remarks></remarks>
Public Class util
    ''' <summary>
    ''' Shared function that returns the current user's access level (stored as user data in the authentication cookie)
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function getUserAccessLevel() As Integer
        Try
            Return CInt(getUserDataItem(1))
        Catch
            Return 0
        End Try
    End Function
    ''' <summary>
    ''' Shared function that returns the current user's ID
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function getUserID() As Integer
        Try
            Return CInt(getUserDataItem(0))
        Catch
            Return 0
        End Try
    End Function
    ''' <summary>
    '''Private function to retrieve items of user data from authentication cookie.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function getUserDataItem(ByVal index As Integer) As String
        Dim myTicket As FormsAuthenticationTicket
        Dim userDataArray() As String
        Try
            'get the forms authentication ticket for the current user
            myTicket = CType(HttpContext.Current.User.Identity, FormsIdentity).Ticket
            'get the user data (comma delineated) and use the split function to convert to string array
            userDataArray = Split(myTicket.UserData, ",")
            'return the relevant array element
            Return userDataArray(index)
        Catch
            'if the user is not authenticated or the value for index is out of range then return null string
            Return ""
        End Try
    End Function

End Class
